<template>
    <div class="address-wrapper" v-if="show" @touchend="stopEvent">
        <div class="address-content">
            <div class="btn-wrapper">
                <text class="btn cancel" @click="cancel">取消</text>
                <text class="btn confirm" @click="confirm">确定</text>
            </div>
            <div class="city-content">
                <list class="listitem">
                    <cell class="cellitem" v-for="(item, index) in addressList" :key="index" @click="chooseProvince(index)">
                        <text class="word" :class="[provinceIndex === index ? 'select' : '']">{{item.text}}</text>
                    </cell>
                </list>
                <list class="listitem" ref="city">
                    <cell class="cellitem" v-for="(item, index) in addressList[provinceIndex].children" :key="index" @click="chooseCity(index)">
                        <text class="word" :class="[cityIndex === index ? 'select' : '']">{{item.text}}</text>
                    </cell>
                </list>
                <list class="listitem" ref="county">
                    <cell class="cellitem" v-for="(item, index) in addressList[provinceIndex].children[cityIndex].children" :key="index"
                          @click="chooseCounty(index)">
                        <text class="word" :class="[countyIndex === index ? 'select' : '']">{{item.text}}</text>
                    </cell>
                </list>
            </div>
        </div>
    </div>
</template>

<script>
import { addressList } from '@/util/address'

const dom = weex.requireModule('dom')

export default {
  name: 'address',
  props: ['show'],
  data () {
    return {
      addressList, // 地址列表
      provinceIndex: 0, // 省下标
      cityIndex: 0, // 城市下标
      countyIndex: 0 // 县下标
    }
  },
  methods: {
    stopEvent (e) { // 阻止穿透
      const { platform } = weex.config.env
      platform === 'Web' && e.preventDefault && e.preventDefault()
    },
    chooseProvince (inx) { // 选择省
      if (this.provinceIndex !== inx) {
        this.provinceIndex = inx
        this.cityIndex = 0
        this.countyIndex = 0
        let els = this.$refs.city.children
        let elsCount = this.$refs.county.children
        dom.scrollToElement(els[this.cityIndex], {})
        dom.scrollToElement(elsCount[this.countyIndex], {})
      }
    },
    chooseCity (inx) { // 选择市
      if (this.cityIndex !== inx) {
        this.cityIndex = inx
        this.countyIndex = 0
        let els = this.$refs.county.children
        dom.scrollToElement(els[this.countyIndex], {})
      }
    },
    chooseCounty (inx) { // 选择县
      this.countyIndex = inx
    },
    cancel () { // 取消
      this.$emit('cancel')
    },
    confirm () { // 确定
      let str = this.addressList[this.provinceIndex].text + ' ' + this.addressList[this.provinceIndex].children[this.cityIndex].text + ' ' +
        this.addressList[this.provinceIndex].children[this.cityIndex].children[this.countyIndex].text
      let val = this.addressList[this.provinceIndex].value + ' ' + this.addressList[this.provinceIndex].children[this.cityIndex].value + ' ' +
        this.addressList[this.provinceIndex].children[this.cityIndex].children[this.countyIndex].value
      let obj = {
        str: str,
        val: val
      }
      this.$emit('confirm', obj)
    }
  }
}
</script>

<style scoped>
    .address-wrapper {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
        background-color: rgba(43, 48, 64, 0.3);
    }
    .address-content {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        height: 704px;
        background-color: #fff;
    }
    .btn-wrapper {
        height: 88px;
        padding-left: 16px;
        padding-right: 16px;
        border-bottom-width: 1px;
        border-bottom-style: solid;
        border-bottom-color: #cecece;
        flex-direction: row;
        justify-content: space-between;
        border-top-left-radius: 20px;
        border-top-right-radius: 20px;
    }
    .btn {
        /*height: 40px;*/
        height: 88px;
        padding-left: 16px;
        padding-right: 16px;
        font-size: 26px;
        line-height: 88px;
    }
    .cancel {
        color: #2b3040;
    }
    .confirm {
        color: #fecb01;
    }
    .city-content {
        flex-direction: row;
        justify-content: space-between;
    }
    .listitem {
        width: 250px;
        height: 616px;
        padding-top: 16px;
        padding-bottom: 16px;
    }
    .word {
        line-height: 88px;
        font-size: 32px;
        color: #464646;
        text-align: center;
    }
    .select {
        color: #fecb01;
    }
</style>
